<script lang="ts" setup>
import pageError from '@/assets/svgs/404.svg'
import networkError from '@/assets/svgs/500.svg'
import noPermission from '@/assets/svgs/403.svg'

defineOptions({ name: 'Error' })

interface ErrorMap {
  url: string
  message: string
  buttonText: string
}

type ErrorType = '404' | '500' | '403'

const errorMap: Record<ErrorType, ErrorMap> = {
  '404': {
    url: pageError,
    message: '抱歉，您访问的页面不存在。',
    buttonText: '返回首页',
  },
  '500': {
    url: networkError,
    message: '抱歉，服务器出错了。',
    buttonText: '返回首页',
  },
  '403': {
    url: noPermission,
    message: '抱歉，您没有权限访问该页面。',
    buttonText: '返回首页',
  },
} as const

const props = withDefaults(
  defineProps<{
    type?: ErrorType
  }>(),
  {
    type: '404',
  }
)

const emit = defineEmits(['errorClick'])

const btnClick = () => {
  emit('errorClick', props.type)
}
</script>

<template>
  <div class="flex justify-center">
    <div class="text-center">
      <img :src="errorMap[type].url" alt="" width="350" />
      <div class="text-14px text-[var(--el-color-info)]">{{
        errorMap[type].message
      }}</div>
      <div class="mt-20px">
        <ElButton type="primary" @click="btnClick">{{
          errorMap[type].buttonText
        }}</ElButton>
      </div>
    </div>
  </div>
</template>
